﻿using PetaPoco;
using PmSoft;
using PmSoft.Caching;
using static Modules.Project.ProjectConstants;

namespace Modules
{
    /// <summary>
    /// 项目状态变更记录表
    ///</summary>
    [TableName("project_status_change_record")]
    [PrimaryKey("id", AutoIncrement = true)]
    [CacheSetting(true, PropertyNamesOfArea = "ProjectId")]
    [Serializable]
    public class ProjectStatusChangeRecord : IEntity
    {
        /// <summary>
        /// 主键
        ///</summary>
        [Column("id")]
        public int Id { get; set; }

        /// <summary>
        /// 项目Id
        /// </summary>
        [Column("project_id")]
        public int ProjectId { get; set; }

        /// <summary>
        /// 变更前状态
        ///</summary>
        [Column("old_status")]
        public 项目状态? OldStatus { get; set; }

        /// <summary>
        /// 变更后状态
        ///</summary>
        [Column("new_status")]
        public 项目状态 NewStatus { get; set; }
        /// <summary>
        /// 生效时间
        /// </summary>
        [Column("effective_date")]
        public DateTime EffectiveDate { get; set; }

        /// <summary>
        /// 创建时间（变更时间）
        ///</summary>
        [Column("create_time")]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 备注
        ///</summary>
        [Column("remark")]
        public string Remark { get; set; }

        #region IEntity 成员

        [Ignore]
        object IEntity.EntityId { get { return this.Id; } }

        #endregion IEntity 成员
    }

    //public static class ProjectStatusChangeRecordExtensions
    //{
    //    /// <summary>
    //    /// 获取项目时间范围内项目状态累计天数
    //    /// </summary>
    //    /// <param name="projectStatusChangeRecords">数据集合，项目id必须相同</param>
    //    /// <param name="status"></param>
    //    /// <param name="startTime">不能跨月份</param>
    //    /// <param name="endTime">不能跨月份</param>
    //    /// <returns></returns>
    //    public static int GetStatusDays(this IEnumerable<ProjectStatusChangeRecord> projectStatusChangeRecords, 项目状态 status,  DateTime startTime, DateTime endTime)
    //    {
    //        int days = 0;

    //        if (projectStatusChangeRecords.Any())
    //        {
    //            //1.查询项目状态记录表更表中，若有数据，则查询最后一次状态
    //            var thisMonthProjectStatusChangeRecord = projectStatusChangeRecords.OrderByDescending(a => a.EffectiveDate);
    //            //当月无数据
    //            if (thisMonthProjectStatusChangeRecord.FirstOrDefault()?.EffectiveDate.Date < startTime.Date)
    //            {
    //                //1.1判断最后一次状态是否为在建，则数量为当月1号到当前统计时间-1的天数
    //                if (thisMonthProjectStatusChangeRecord.FirstOrDefault()?.NewStatus == status)
    //                {
    //                    days = (endTime - startTime).Days;
    //                }
    //                //若不为在建，则在建天数为0
    //                else
    //                {
    //                    days = 0;
    //                }
    //            }
    //            //2.当月有数据，则判断当月之前最后一条数据状态是否为在建
    //            else
    //            {
    //                var lastMonthProjectStatusChangeRecord = thisMonthProjectStatusChangeRecord.Where(a => a.EffectiveDate.Date == startTime.Date).OrderBy(a => a.EffectiveDate).ToList();
    //                for (int i = 0; i < lastMonthProjectStatusChangeRecord.Count(); i++)
    //                {
    //                    //查询第一条数据的旧状态是否为在建
    //                    if (i == 0)
    //                    {
    //                        //若为在建，则查询当月第一条数据，用当月第一条的日期作为当月在建天数的第一阶段
    //                        if (lastMonthProjectStatusChangeRecord[i].OldStatus == status)
    //                        {
    //                            days += (lastMonthProjectStatusChangeRecord[i].EffectiveDate - startTime).Days;
    //                        }
    //                        else if (lastMonthProjectStatusChangeRecord[i].NewStatus == status)
    //                        {
    //                            days += (endTime - lastMonthProjectStatusChangeRecord[i].EffectiveDate).Days;
    //                        }
    //                    }
    //                    else if (i > 0 && lastMonthProjectStatusChangeRecord[i].NewStatus == status && i < lastMonthProjectStatusChangeRecord.Count() - 1)
    //                    {
    //                        days += (lastMonthProjectStatusChangeRecord[i + 1].EffectiveDate - lastMonthProjectStatusChangeRecord[i].EffectiveDate).Days;
    //                    }
    //                    else if (lastMonthProjectStatusChangeRecord[i].NewStatus == status && i == lastMonthProjectStatusChangeRecord.Count() - 1)
    //                    {
    //                        days += (endTime - lastMonthProjectStatusChangeRecord[i].EffectiveDate).Days;
    //                    }
    //                }
    //            }
    //        }
 

    //        return days;
    //    }
    //}
}